package com.mukefile.action;

import com.mukefile.pojo.MukeFile;
import com.mukefile.pojo.User;
import com.mukefile.service.MukeFileService;
import com.mukefile.utils.GetPathProperties;
import com.mukefile.utils.IsLogin;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: kenewstar
 * @Description: 文件操作控制器
 * @Date:Created in 2020/5/5
 */
@Controller("mukeAction")
@Scope("prototype")
public class MukeFileAction extends ActionSupport implements ModelDriven<MukeFile> {

    @Autowired
    private MukeFileService mukeFileService;
    /**
     * 文件
     */
    private File file;
    /**
     * 文件名
     */
    private String fileFileName;
    /**
     * 获取session
     */
    private final Map<String,Object> session = ActionContext.getContext().getSession();
    /**
     * 获取request
     */
    private final Map<String, Object> request = (Map<String, Object>)ActionContext.getContext().get("request");
    public File getFile() {
        return file;
    }
    public void setFile(File file) {
        this.file = file;
    }
    public String getFileFileName() {
        return fileFileName;
    }
    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }

    private MukeFile mukeFile = new MukeFile();

    @Override
    public MukeFile getModel() {
        return this.mukeFile;
    }

    /**
     * 上传文件
     * @return
     */
    public String upload(){

        //获取用户
        User user = (User) session.get("user");
        //修改可用空间大小
        user.setDiskAvail(user.getDiskAvail()-this.file.length());
        //添加MukeFile内容
        mukeFile.setFileName(this.fileFileName);
        mukeFile.setParentId(0);
        mukeFile.setFileSize((double)this.file.length());
        mukeFile.setUploadTime(new Date());
        user.getMukeFiles().add(mukeFile);
        mukeFile.setUser(user);
        int i = this.mukeFileService.fileUpload(this.file, mukeFile);
        if (i==0){
            //文件上传失败
            //将user的信息改回
            user.setDiskAvail(user.getDiskAvail()+mukeFile.getFileSize());
            session.put("user",user);
            request.put("msg","文件上传失败！");
        }else {
            request.put("msg","文件上传成功");
        }
        return "upload";
    }

    /**
     * 显示所有文件
     * @return 返回所有文件信息
     */
    public String showAllFile(){

        User user = (User)this.session.get("user");
        List<MukeFile> files = this.mukeFileService.findAllFile(user);
        if(files.isEmpty()){
            //没有文件
            request.put("msg","您的磁盘中暂无文件!");
        }
        request.put("list",files);
        return "fileOperate";
    }
    @Autowired
    private GetPathProperties getPathProperties;
    private InputStream inputStream;

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    /**
     * 文件下载
     * @return
     * @throws FileNotFoundException
     */
    public String download() throws Exception {
        //获取用户信息
        User user = (User)this.session.get("user");
        //打印下载文件信息
        this.mukeFileService.download(user,mukeFile.getFileName());
        //下载文件
        String path= getPathProperties.getDiskRootPath()+File.separator+user.getDiskRootPath();
        inputStream = new FileInputStream(new File(path + File.separator + "DiskA"
                + File.separator,mukeFile.getFileName()));
        mukeFile.setFileName(new String(mukeFile.getFileName().getBytes(),"ISO8859-1"));
        return SUCCESS;
    }

    /**
     * 文件删除
     * @return 返回到fileOperate.ftl
     */
    public String deleteFile(){
        Double fileSize = this.mukeFile.getFileSize();
        User user = (User) this.session.get("user");
        user.setDiskAvail(user.getDiskAvail()+fileSize);
        this.mukeFile.setUser(user);
        int i = this.mukeFileService.deleteFile(mukeFile);
        if (i==0){
            user.setDiskAvail(user.getDiskAvail()-fileSize);
            this.session.put("user",user);
            this.request.put("msg","文件删除失败");
        }else {
            this.request.put("msg","文件删除成功");
        }
        List<MukeFile> files = this.mukeFileService.findAllFile(user);
        this.request.put("list",files);
        return "fileDelete";
    }

    /**
     * 显示上传页面
     * @return 页面
     */
    public String showUpload(){
        return "showUpload";
    }
}
